﻿var path = this.path;
define([this.path + './../4.x/plugins/esri/layers/DivGraphicsLayer.js'], function(GraphicsLayer) {
    var Layer = GraphicsLayer.createSubclass({
        setData: function(mapView, layer) {
            var me = this;
            me.imageUrl = path + './../../one-map/1.0.0/src/mode/arcgis/css/images/square.svg';
            var options = layer.layer || {};
            me.mapView = mapView;
            let mapUtils = me.mapUtils = mapView.mapUtils;
            me.options = options;
            me.layer = layer.layer;
            me.spatialReference = mapView.getSpatialReference()


            if (!this._element) {
                return;
            };
            var lngLatArray = mapUtils.toLngLatArray(mapView, options.data, options);
            this.data = lngLatArray;
            this._setData(this.data)
        },
        _setData: function(lngLatArray) {
            var me = this;
            lngLatArray.map((item, index) => {
                this.createGraphic(item);
                if (index == lngLatArray.length - 1) {
                    if (typeof me.options.callback == 'function') {
                        me.options.callback(me.layer);
                    };
                };
            });
        },
        createGraphic: function(item) {
            var me = this;
            var symbolFieldName = this.options.symbolFieldName || this.mapView.getSymbolFieldName();
            var point = me.mapUtils.createPoint(me.mapView, item);
            var symbol = item[symbolFieldName] || me.options.symbol || {};
            var divGraphic = me.mapUtils.createGraphic(me.mapView, {
                geometry: point,
                attributes: item,
                popupTemplate: null
            });

            me._setElement(divGraphic);
            var eleJQ = $(me._getElement(divGraphic));
            divGraphic.attributes = item;
            var _elementJQ = eleJQ[0];
            if (symbol.width) {
                _elementJQ.width(symbol.width);
            };
            if (symbol.height) {
                _elementJQ.height(symbol.height);
            };


            me.mapUtils.ComjsUtils.openInElement(_elementJQ, Object.assign({
                onLoaded: function() {
                    me._draw(divGraphic);
                    me.add(divGraphic);
                }
            }, symbol));
        },
        getGraphicLocation: function(mapView, esriEvent) {
            if (!esriEvent || !esriEvent.graphic) {
                return;
            };
            if (!mapView) {
                return;
            };

            var geometry = esriEvent.graphic.geometry || {};
            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {}
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            return location;
        },
        convertSr: function(mapView, lngLatArray, callback) {
            var me = this;
            if (!mapView) {
                return;
            };
            var mapUtils = mapView.mapUtils;
            var mapSr = mapView.getSpatialReference();
            me.mapSr = mapSr;
            var layerSr = me.spatialReference;
            if (mapSr == layerSr) {
                callback();
                return
            };
            /*坐标转换，扩展的图层不像svg可以自动转换坐标，如果点位的坐标与底图的坐标不一致需要先手工转换坐标*/
            if (mapSr.wkid == '102100') {
                if (layerSr.wkid == '4326' || layerSr.wkid == '4490') {
                    me.lngLatToXY(mapView, lngLatArray, function(lngLatArray) {
                        if ($.isFunction(callback)) {
                            callback(lngLatArray, mapSr);
                        };
                    });
                    return;
                };
            };
            if (mapSr.wkid == '4326' || mapSr.wkid == '4490') {
                if (layerSr.wkid == '102100') {
                    me.xyToLngLat(mapView, lngLatArray, function(lngLatArray) {
                        if ($.isFunction(callback)) {
                            callback(lngLatArray, mapSr);
                        };
                    });
                    return;
                };

            };
            if ($.isFunction(callback)) {
                callback(lngLatArray, me.spatialReference);
            };

        },
        lngLatToXY: function(mapView, lngLatArray, callback) {
            mapView.mapUtils.webMercatorUtils(mapView, lngLatArray, { isLngLat: true, callback: callback });
        },
        xyToLngLat: function(mapView, data, callback) {
            mapView.mapUtils.webMercatorUtils(mapView, lngLatArray, { isLngLat: false, callback: callback });
        }

    })
    return Layer;
});